package com.neuedu.his.controller;

import com.neuedu.his.po.Checkapply;
import com.neuedu.his.po.Fmeditem;
import com.neuedu.his.service.CheckapplyService;
import com.neuedu.his.service.FmeditemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/check")
public class CheckApplyController {

    @Autowired
    CheckapplyService service;

    @Autowired
    FmeditemService Fmedservice;

    @RequestMapping("/listpaid")
    public List<Checkapply> listpaid() {
        QueryWrapper<Checkapply> qw = new QueryWrapper<>();
        qw.eq("state",2);
        return service.list(qw);
    }

    @RequestMapping("/getbymid")
    public Map<String,Object> getbymid(@RequestParam Integer mid){
        return service.getinfoAndPriceByMedicalID(mid);
    }

    @RequestMapping("/searchcheck")
    public List<Fmeditem> searchcheck(@RequestBody Map<String,Object> params){
        String itemname = (String) params.get("itemname");
        Integer deptid = (Integer) params.get("deptid");
        QueryWrapper<Fmeditem> queryWrapper = new QueryWrapper<>();
        if((itemname==null || itemname.equals(""))&&(deptid==null)){
            return Fmedservice.list();
        } else if (itemname==null || itemname.equals("")){
            queryWrapper.eq("deptid",deptid);
            return Fmedservice.list(queryWrapper);
        } else if (deptid==null) {
            queryWrapper.like("itemname", itemname);
            return Fmedservice.list(queryWrapper);
        }
        queryWrapper.like("itemname", itemname).eq("deptid",deptid);
        return Fmedservice.list(queryWrapper);
    }

    @RequestMapping("/addapply")
    public boolean addapply(@RequestBody List<Checkapply> checkApplyList) {
        // 设置默认值
        checkApplyList.forEach(item -> {
            item.setState(1);
        });
        // 直接使用MyBatis-Plus的默认批量保存方法
        return service.saveBatch(checkApplyList);
    }

    @RequestMapping("/getcheck")
    public Checkapply getcheck(){
        return new Checkapply();
    }

    @RequestMapping("/getfmed")
    public Fmeditem getfmed(){
        return new Fmeditem();
    }

    @RequestMapping("/listbyregistid")
    public List<Checkapply> listbyregistid(@RequestParam Integer registid){
        QueryWrapper<Checkapply> qw = new QueryWrapper<>();
        qw.eq("registid",registid);
        return service.list(qw) ;
    }

    @RequestMapping("/listbymedicalid")
    public List<Checkapply> listbymedicalid(@RequestParam Integer medicalid){
        QueryWrapper<Checkapply> qw = new QueryWrapper<>();
        qw.eq("medicalid",medicalid);
        return service.list(qw) ;
    }

    @RequestMapping("/checkout")
    public boolean checkout(@RequestBody List<Checkapply> checkApplyList){
        if (checkApplyList == null || checkApplyList.isEmpty()) {
            return false;
        }
        // 遍历列表，设置每个元素的state为2
        for (Checkapply item : checkApplyList) {
            item.setState(2);
        }
        // 使用MyBatis-Plus的批量更新方法
        return service.updateBatchById(checkApplyList);
    }
    @RequestMapping("/checkcompleted")
    public boolean checkcompleted(@RequestBody List<Checkapply> checkApplyList){
        if (checkApplyList == null || checkApplyList.isEmpty()) {
            return false;
        }
        for (Checkapply item : checkApplyList) {
            item.setState(3);
            item.setChecktime(new Date());
        }
        // 使用MyBatis-Plus的批量更新方法
        return service.updateBatchById(checkApplyList);
    }
    @RequestMapping("/allcompleted")
    public boolean allcompleted(@RequestBody List<Checkapply> selectedlastCheckItems){
        if (selectedlastCheckItems == null || selectedlastCheckItems.isEmpty()) {
            return false;
        }
        for (Checkapply item : selectedlastCheckItems) {
            item.setState(4);
            item.setResulttime(new Date());
        }
        // 使用MyBatis-Plus的批量更新方法
        return service.updateBatchById(selectedlastCheckItems);
    }
}
